﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FlexiCommerce.Web.Templates.Web;
using FlexiCommerce.Web.Templates.Web.UI.WebControls;
using FlexiCommerce.Web.Templates.Layouts;
using N2.Engine.Globalization;
using N2.Web;
using FlexiCommerce.Components;
using N2.Integrity;
using N2;
using N2.Collections;

namespace FlexiCommerce.Web.Addons.FC.Items
{
    [StoreTemplate("Category")]
    [Definition("Category", "CategoryPage")]
    [RestrictParents(new Type[] { typeof(StorePage) })]
    public class CategoryPage : AbstractStorePage, IPrependingPaths, ITranslationProvider
    {

    

        public override bool Visible
        {
            get
            {
                return false;
            }
        }

        #region IPrependingPaths Members

        public System.Collections.Generic.IList<N2.Web.ILink> GetPaths
        {
            get
            {
                IList<ILink> list = new List<ILink>();
                foreach (Category c in Find.EnumerateParents<Category>(Context.CurrentCategory))
                {
                    list.Add(new Link(c.Name, c.Name, string.Empty, Context.UrlParser.BuildUrl(c)));
                }
                return list;
            }
        }

        #endregion

        #region ITranslationProvider Members

        public IEnumerable<Translation> GetTranslations()
        {
            if (Context.CurrentCategory != null)
            {
                IList<Translation> list = new List<Translation>();

                foreach (ILanguage language in N2.Context.Current.Resolve<ILanguageGateway>().GetAvailableLanguages())
                {
                    list.Add(new Translation(null, language, Web.Url.ToRelative(new Url("~/flexicommerce/translation.ashx").AppendQuery("categoryid", Context.CurrentCategory.ID).AppendQuery("language", language.LanguageCode))));
                }
                return list;
            }
            else
            {
                return GetPageTranslations();
            }
        }

        #endregion

        public override PathData FindPath(string remainingUrl)
        {
            //PathData pathData = base.FindPath(remainingUrl);
            //pathData.Argument = Context.CurrentCategory.ID.ToString();
            //return pathData;
            return new PathData(this, "~/Addons/FlexiCommerce/UI/Views/Category.aspx", "category", remainingUrl);
        }
        private IEnumerable<Translation> GetPageTranslations()
        {
            ILanguageGateway languages = N2.Context.Current.Resolve<ILanguageGateway>();
            ItemFilter languageFilter = new CompositeFilter(new AccessFilter(), new PublishedFilter());
            IEnumerable<ContentItem> translations = languages.FindTranslations(this);
            foreach (ContentItem translation in languageFilter.Pipe(translations))
            {
                ILanguage language = languages.GetLanguage(translation);

                // Hide translations when filtered access to their language
                ContentItem languageItem = language as ContentItem;
                if (languageItem == null || languageFilter.Match(languageItem))
                    yield return new Translation(translation, language);
            }
        }

    }

    //[StoreTemplate("Category")]
    //[Definition("Category","CategoryPage")]
    //[RestrictParents(new Type[] {typeof(StorePage)})]
    //public class CategoryPage: AbstractStorePage, IPrependingPaths, ITranslationProvider
    //{

    //    public override string Name
    //    {
    //        get
    //        {
    //            if (Context.CurrentCategory == null)
    //            {
    //                return Web.Url.Format(Context.LocalizationManager.GetResource("CATEGORY"));
    //            }
    //            else
    //            {
    //                return Web.Url.Format(Context.LocalizationManager.GetResource("CATEGORY") + "-" + Context.CurrentCategory.Name + "_" + Context.CurrentCategory.ID.ToString());
    //            }
    //        }
    //    }

    //    protected override bool Equals(string name)
    //    {
    //        return Context.UrlParser.CategoryRegex.IsMatch(name) || Name.Equals(name, StringComparison.InvariantCultureIgnoreCase)
    //            || (Name + Extension).Equals(name, StringComparison.InvariantCultureIgnoreCase); ;
    //    }

    //    public override string Title
    //    {
    //        get
    //        {
    //            if (Context.CurrentCategory == null)
    //            {
    //                return base.Title;
    //            }
    //            else
    //            {
    //                return Context.CurrentCategory.Name;
    //            }
    //        }

    //    }

    //    public override bool Visible
    //    {
    //        get
    //        {
    //            return false;
    //        }
    //    }

    //    #region IPrependingPaths Members

    //    public System.Collections.Generic.IList<N2.Web.ILink> GetPaths
    //    {
    //        get
    //        {
    //            IList<ILink> list = new List<ILink>();
    //            foreach (Category c in Find.EnumerateParents<Category>(Context.CurrentCategory))
    //            {
    //                list.Add(new Link(c.Name, c.Name, string.Empty, Context.UrlParser.BuildUrl(c)));
    //            }
    //            return list;
    //        }
    //    }

    //    #endregion

    //    #region ITranslationProvider Members

    //    public IEnumerable<Translation> GetTranslations()
    //    {
    //        IList<Translation> list = new List<Translation>();

    //        foreach (ILanguage language in N2.Context.Current.Resolve<ILanguageGateway>().GetAvailableLanguages())
    //        {
    //            list.Add(new Translation(null, language, Web.Url.ToRelative(new Url("~/flexicommerce/translation.ashx").AppendQuery("itemid", Context.CurrentCategory.ID).AppendQuery("language", language.LanguageCode))));
    //        }
    //        return list;
    //    }

    //    #endregion

    //    public override PathData FindPath(string remainingUrl)
    //    {
    //        //PathData pathData = base.FindPath(remainingUrl);
    //        //pathData.Argument = Context.CurrentCategory.ID.ToString();
    //        //return pathData;
    //        return new PathData(this, "~/Addons/FlexiCommerce/UI/Views/Category.aspx", "submit", "arg");
    //    }

    //}
}
